package t1006;

import java.util.Scanner;

/**
 * @姓名 张东升
 * @日期 2025年03月2025/3/25日、
 */
public class Main {
    public static int linkListCount;

    //12 32 42 53 6 43 0
    public static void main(String[] args) {
        Node head = new Node();
        createLinkList(head);
        sort(head);
        display(head);
        reserve(head);
        display(head);

    }

    //插入到最后元素
    public static void insert(Node head, int element){
        Node next=  head.next;
        while (next.next!=null){
            next = next.next;
        }
        //找到最后一个元素,插入
        next.next = new Node(element,null);
    }

    public static void reserve(Node head){
        Node prev = null;
        Node current = head.next;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        head.next = prev;
    }
 // 排序单链表，从小到大
    public static void sort(Node head){
        Node pre = head.next;
        Node next =  pre.next;
        for (int i = 0; i < linkListCount; i++) {
            while (next!= null){
                if (pre.data > next.data){
                    int temp = pre.data;
                    pre.data = next.data;
                    next.data = temp;
                }
                pre = next;
                next = next.next;
            }
            pre = head.next;
            next =  pre.next;;
        }
    }


    public static void createLinkList(Node head){
        int count = 0;
        Node temp = head;
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n!=0){
            temp.next = new Node(n,null);
            temp = temp.next;
            n = scanner.nextInt();
            count++;
        }
        linkListCount = count;
    }
    public static void display( Node head){
        Node next = head.next;
        System.out.print("Data: ");
        while (next!=null){
            System.out.print(next.data + " ");
            next = next.next;
        }
        System.out.println();
    }
    public static Node getEndNode(Node head  ,Node end){
        Node node = head.next;
        Node res = null;
        Node pre = null;
        while (node!=null){

            if (node.next == end && end == null){
                res = pre;
                break;
            }

            if (node.next == end){
                res = node;
                break;
            }


            pre = node;
            node = node.next;
        }
        return res  ;
    }
}



class Node{
    public int data;
    public
    Node next;

    public Node() {
    }

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }
}

